﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace AngryNerds
{
    public class PhysicsEngine
    {
        public static float CalculateAngleInRadian(Vector2 pointToCup, Vector2 pointToBase)
        {
            Vector2 thirdPoint = new Vector2(pointToBase.X+50,pointToBase.Y); // Açıyı hesaplayabilmemiz için gerekli olan 3. noktayı pointToBase noktasının yatayındaki herhangi bir noktadan seçiyoruz

            double edge1 = Math.Sqrt(Math.Pow(pointToCup.X-pointToBase.X,2)+Math.Pow(pointToCup.Y-pointToBase.Y,2));
            double edge2 = Math.Sqrt(Math.Pow(pointToBase.X-thirdPoint.X,2)+Math.Pow(pointToBase.Y-thirdPoint.Y,2));
            double edge3 = Math.Sqrt(Math.Pow(pointToCup.X-thirdPoint.X,2)+Math.Pow(pointToCup.Y-thirdPoint.Y,2));

            double angle = Math.Acos((edge1*edge1+edge2*edge2-edge3*edge3)/(2*edge1*edge2));

            return (float)angle;
        }

        public static float _CalculateVelocity(float angle)
        {
            return 50*angle;
        }

        private static float _CalculateX(float velocity,float angle,float time)
        {
            return (velocity * (float)Math.Cos(angle) * time);
        }

        private static float _CalculateY(float velocity,float angle,float time)
        {
            return (velocity * (float)Math.Sin(angle)*time  - 5 * time * time);
        }

        public static Vector2 CalculateCurrentPoint(Vector2 initialPoint,float velocity,float angle,float time)
        {
            return new Vector2(initialPoint.X+_CalculateX(velocity,angle,time),initialPoint.Y-_CalculateY(velocity,angle,time));
        }

        public static void CheckCollison(GameObject gameObject1, GameObject gameObject2)
        {
            if (gameObject1.BoundingRectangle.Intersects(gameObject2.BoundingRectangle))
            {
                gameObject1.OnCollide();
                gameObject2.OnCollide();
            }
        }

        public static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
                            Rectangle rectangleB, Color[] dataB)
        {
            // Dikdörtgenlerin sınırları bulunuyor
            int top = Math.Max(rectangleA.Top, rectangleB.Top);
            int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
            int left = Math.Max(rectangleA.Left, rectangleB.Left);
            int right = Math.Min(rectangleA.Right, rectangleB.Right);

            int totalIntersectedPixel = 0,totalPixel=0;

            // Kesişimin her bir noktasını kontrol et
            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    // Belirtilen noktalardaki renkleri al
                    Color colorA = dataA[(x - rectangleA.Left) +
                                         (y - rectangleA.Top) * rectangleA.Width];
                    Color colorB = dataB[(x - rectangleB.Left) +
                                         (y - rectangleB.Top) * rectangleB.Width];

                    totalPixel++;
                    // Eğer piksellerin her ikisi de trasparan değilse,
                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        totalIntersectedPixel++;
                    }
                }
            }

            if (totalIntersectedPixel > totalPixel / 30)
                return true;
            
            // kesişim yok ya da yeterli miktarda değil
            return false;
        }

    }
}